//
//  Kratos_QuickLoginPresenter.m
//  kk_framework
//
//  Created by 7yao.top on 2018/11/20.
//  Copyright © 2018年 7yao.top. All rights reserved.
//

#import "Kratos_QuickLoginPresenter_Kra.h"
#import "Kratos_QuickView.h"
#import "Kratos_phoneinput.h"
#import "Kratos_pwdinput.h"
#import "Kratos_QuickInputView.h"
#import "Kratos_userInfo.h"
#import "DCTrackingAgent.h"
#import "Kratos_Tool.h"
#import "UIView+Toast.h"
#import "Kratos_longzhuInputView.h"

@interface Kratos_QuickLoginPresenter_Kra() <Kratos_QuickLoginViewDelegate>
@property (nonatomic,weak) UIViewController *weakVC;
@end;

@implementation Kratos_QuickLoginPresenter_Kra
- (void)Kratos_usageLogic
{
    if ([self.view1 isKindOfClass:[Kratos_QuickView class]]) {
        Kratos_QuickView *quickView =  (Kratos_QuickView *)self.view1;
        quickView.delegate  = self;
    }
    ///请求相册访问权限
    [Kratos_Tool requestPhoto];
    
    [self Kratos_getRandomAccound];
    
    
}
- (void)Kratos_getRandomAccound
{
    
    Kratos_QuickView *quickView =  (Kratos_QuickView *)self.view1;
    
    [self.Kra_httpClient Kratos_noGeneralpost:@"/user/pub/randuserpwdapi" Kratos_parameters:nil onSuccess:^(Kratos_HttpResponse * _Nullable response) {
        if (response.Kra_status == Kratos_HttpResponseStatusSuccess)
        {
            
            if ([response.Kra_content isKindOfClass:[NSDictionary class]])
            {
                NSDictionary *dict = response.Kra_content;
                if (dict[@"username"] != nil) {
                    quickView.quickInputView.phoneInput.Kra_textField.text = dict[@"username"];

                }
                if (dict[@"password"] != nil) {
                    if ( [dict[@"password"] integerValue] > 0)
                    {
                        quickView.quickInputView.pwdinput.Kra_textField.text = [NSString stringWithFormat:@"%ld",[dict[@"password"] integerValue]];
                        quickView.quickInputView.pwdinput.Kra_isDisPlay.selected = true;
                        [quickView.quickInputView.pwdinput Kratos_isDisPlayClick:quickView.quickInputView.pwdinput.Kra_isDisPlay];
                    }
                    

                }
                
            }
            
            
        }
    } onFail:^(Kratos_HttpResponse * _Nullable response) {
        if (response.Kra_status == Kratos_HttpResponseStatusError) {
        }
    }];
}



- (void)setViewController:(UIViewController *)ViewController
{
    self.weakVC = ViewController;
}



- (void)Kratos_dissmissVC {
    [self.weakVC dismissViewControllerAnimated:false completion:nil];
}

- (void)Kratos_loginClick:(UIButton *)btn {
    
    __weak typeof(self.view1) weakView = self.view1;
    __weak typeof(self) weakself = self;

    Kratos_QuickView *quickView =  (Kratos_QuickView *)weakView;
    NSString *userName = quickView.quickInputView.phoneInput.Kra_textField.text;
    NSString *pwd = quickView.quickInputView.pwdinput.Kra_textField.text;
    
    
    
    if (userName == nil || userName.length == 0) {
        [quickView makeToast:@"账号不存在"];
        return;
    }
    
    if (pwd == nil || pwd.length == 0) {
        [quickView makeToast:@"密码不存在"];
        return;
    }
    
    
    if (quickView.agreeniSSelect == false) {
        [quickView makeToast:@"必须同意协议才能注册"];
        return;
    }
    
    
    
    
    [self.Kra_httpClient Kratos_post:@"/user/pub/accountpwdapi" Kratos_parameters:@{@"username":userName,@"password":pwd,@"auto_reg":@"1"} Kratos_onSuccess:^(Kratos_HttpResponse * _Nullable response) {
        if (response.Kra_status == Kratos_HttpResponseStatusSuccess)
        {
            if ([response.Kra_content[@"code"] integerValue] == 1)
            {
                Kratos_userInfo *userInfo =  [[Kratos_userInfo alloc] initWithDict:response.Kra_content[@"data"]];
                [DCTrackingPoint createAccount:userInfo.Kra_ID];
                userInfo.Kra_account = userName;
                userInfo.Kra_pwd = pwd;
                [userInfo Kratos_saveinfo_Kratos];
                
                [Kratos_Tool Kratos_checkPhotoStauts:^(PHAuthorizationStatus photoAuthorStatus) {
                    
                    if (photoAuthorStatus != PHAuthorizationStatusAuthorized) {
                        
                        UIAlertController *avc = [UIAlertController alertControllerWithTitle:@"提示" message:@"你没有开启相册访问权限，这导致不能保存账号和密码到系统相册中,请在设置打开访问相册权限" preferredStyle:UIAlertControllerStyleAlert];
                        UIAlertAction *action = [UIAlertAction actionWithTitle:@"继续" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                            [weakself.weakVC dismissViewControllerAnimated:false completion:^{
                                
                                quickView.quickInputView.phoneInput.Kra_caller =   NSStringFromClass([Kratos_longzhuInputView class]);
                                [quickView.quickInputView.phoneInput Kratos_savePhoneInCache];
                                [[NSNotificationCenter defaultCenter] postNotificationName:Kra_kLoginSuccessNotification object:userInfo];
                            }];
                            
                            
                        }];
                        
                        
                        [avc addAction:action];
                        [weakself.weakVC presentViewController:avc animated:true completion:nil];
                        
                        return;
                    }
                    
                    [Kratos_Tool Kratos_getScreenImageWithView:quickView Kratos_completionHandle:^(NSError *error, NSString *identifier) {
                        if (!error) {
                            kLog(@"保存截图成功");
                       
                            
                            UIAlertController *avc = [UIAlertController alertControllerWithTitle:@"提示" message:@"你的账号密码已经保存到系统相册中" preferredStyle:UIAlertControllerStyleAlert];
                            UIAlertAction *action = [UIAlertAction actionWithTitle:@"好的" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                                [weakself.weakVC dismissViewControllerAnimated:false completion:^{
                                  
                                    quickView.quickInputView.phoneInput.Kra_caller =   NSStringFromClass([Kratos_longzhuInputView class]);
                                    [quickView.quickInputView.phoneInput Kratos_savePhoneInCache];
                                    [[NSNotificationCenter defaultCenter] postNotificationName:Kra_kLoginSuccessNotification object:userInfo];
                                }];
                                
                                
                            }];
                            [avc addAction:action];
                            [weakself.weakVC presentViewController:avc animated:true completion:nil];
                        }
                        
                    }];
                    
                }];
          
                
                
            }else
            {
                
            }
        }
        
        
    } Kratos_onFail:^(Kratos_HttpResponse * _Nullable response) {
        
    }];
}

- (void)dissmissVC {
    
}



- (void)Kratos_onFail:(id)clientInfo Kratos_errCode:(NSInteger)errCode errInfo:(NSString *)errInfo {
    
}

- (void)Kratos_onSuccess:(id)responseObject {
    
}

@end
